<?php

App::uses('AppController', 'Controller');

class ReportarticlesController extends AppController {

    public $uses = array('Article');

    public function index() {
        
    }

    public function report1_pdf() {
        $this->layout = 'pdf';
        $this->response->type('pdf');
        $this->autoRender = false;

        $this->Article->recursive = 0;
        $articles = $this->Article->find('all');

        App::import('Vendor', 'PDF');
        $pdf = new PDF();
        $pdf->title = 'Report de Lista de Precios';
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(30, 5, utf8_decode('Nombre'), 1);
        $pdf->Cell(30, 5, utf8_decode('Modelo'), 1);
        $pdf->Cell(30, 5, utf8_decode('Marca'), 1);
        $pdf->Cell(30, 5, utf8_decode('Precio Venta'), 1);

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 10);
        foreach ($articles as $article) {
            $pdf->Cell(30, 5, utf8_decode($article['Article']['name']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Article']['model']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Brand']['name']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Article']['price_sale']), 1);

            $pdf->Ln();
        }
        $pdf->Output();
    }

    public function report1_excel() {
        $this->autoRender = false;
        $this->Article->recursive = 0;
        $articles = $this->Article->find('all');

        App::import('Vendor', 'PHPExcel_php/PHPExcel');
        App::import('Vendor', 'PHPExcel_php/PHPExcel/Writer/Excel2007');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('Report de Lista de Precios');
        $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'Nombre');
        $objPHPExcel->getActiveSheet()->SetCellValue('C2', 'Modelo');
        $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'Marca');
        $objPHPExcel->getActiveSheet()->SetCellValue('E2', 'Precio Venta');


        $count = 3;
        foreach ($articles as $article) {
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $count, $article['Article']['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $count, $article['Article']['model']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $count, $article['Brand']['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $count, $article['Article']['price_sale']);

            $count++;
        }

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="Report de Lista de Precios.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
    }

    public function report2_pdf() {
        $this->layout = 'pdf';
        $this->response->type('pdf');
        $this->autoRender = false;

        $this->Article->recursive = 0;
        $articles = $this->Article->find('all');

        App::import('Vendor', 'PDF');
        $pdf = new PDF();
        $pdf->title = 'Report de Lista de Stock';
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(30, 5, utf8_decode('Nombre'), 1);
        $pdf->Cell(30, 5, utf8_decode('Modelo'), 1);
        $pdf->Cell(30, 5, utf8_decode('Marca'), 1);
        $pdf->Cell(30, 5, utf8_decode('Stock Minimo'), 1);
        $pdf->Cell(30, 5, utf8_decode('Stock'), 1);

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 10);
        foreach ($articles as $article) {
            $pdf->Cell(30, 5, utf8_decode($article['Article']['name']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Article']['model']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Brand']['name']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Article']['stock_minimum']), 1);
            $pdf->Cell(30, 5, utf8_decode($article['Article']['stock']), 1);

            $pdf->Ln();
        }
        $pdf->Output();
    }

    public function report2_excel() {
        $this->autoRender = false;
        $this->Article->recursive = 0;
        $articles = $this->Article->find('all');

        App::import('Vendor', 'PHPExcel_php/PHPExcel');
        App::import('Vendor', 'PHPExcel_php/PHPExcel/Writer/Excel2007');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('Report de Lista de Stock');
        $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'Nombre');
        $objPHPExcel->getActiveSheet()->SetCellValue('C2', 'Modelo');
        $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'Marca');
        $objPHPExcel->getActiveSheet()->SetCellValue('E2', 'Stock Minimo');
        $objPHPExcel->getActiveSheet()->SetCellValue('F2', 'Stock');


        $count = 4;
        foreach ($articles as $article) {
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $count, $article['Article']['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $count, $article['Article']['model']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $count, $article['Brand']['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $count, $article['Article']['stock_minimum']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $count, $article['Article']['stock']);

            $count++;
        }

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="Report de Lista de Stock.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
    }

}